package net.wyf.myaidlclient;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Handler;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import net.wyf.myaidlserver.ICallListener;
import net.wyf.myaidlserver.IMyAidlInterface;
import net.wyf.myaidlserver.User;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    private Button clickButton, testButton;
    private TextView showTextView, showTestTextView;

    private String action = "net.wyf.myaidlserver.service.MyService";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        handler = new Handler();

        setContentView(R.layout.activity_main);
        clickButton = findViewById(R.id.clickId);
        showTextView = findViewById(R.id.answerId);

        testButton = findViewById(R.id.testId);
        showTestTextView = findViewById(R.id.showTest);

        testButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (aidlInterface != null) {
                    try {
                        aidlInterface.testLister();
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }
                }
            }
        });

        bind();
        clickButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (aidlInterface != null) {
                    try {
                        double add = aidlInterface.add(1.3, 2.4);
                        Log.d(TAG, "onClick: " + add);
                        showTextView.setText(add + "\n");
                        String info1 = aidlInterface.inUserInfo(new User(1, "Jim"));
                        String info2 = aidlInterface.outUserInfo(new User(2, "Lida"));
                        String info3 = aidlInterface.inOutUserInfo(new User(3, "Tom"));
                        Log.d(TAG, "onClick: " + aidlInterface.getData());
                        showTextView.append(info1 + "\n" + "===========line=========" + "\n" + info2 + "\n" +
                                "===========line=========" + "\n" + info3);
                    } catch (RemoteException e) {
                        e.printStackTrace();
                        showTextView.setText("fail");
                    }
                } else {
                    Log.e(TAG, "fail");
                    showTextView.setText("fail");
                }

            }
        });
    }

    IMyAidlInterface aidlInterface;

    public IBinder.DeathRecipient mDeathRecipient = new IBinder.DeathRecipient() {
        @Override
        public void binderDied() {
            if (null == aidlInterface) {
                return;
            }
            aidlInterface.asBinder().unlinkToDeath(mDeathRecipient, 0);
            aidlInterface = null;
            Log.i(TAG, "binderDied ");
            bind();
        }
    };

    private Handler handler;

    ICallListener listener = new ICallListener.Stub() {
        @Override
        public void onMessage(final String message) throws RemoteException {
            Log.d(TAG, "onMessage: "+message);
            handler.post(new Runnable() {
                @Override
                public void run() {
                    showTestTextView.setText(message);
                }
            });

        }
    };

    private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            aidlInterface = IMyAidlInterface.Stub.asInterface(iBinder);
            try {
                iBinder.linkToDeath(mDeathRecipient, 0);
                aidlInterface.registListener(listener);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            Log.e(TAG, "onServiceDisconnected: ");
            if (aidlInterface != null) {
                try {
                    aidlInterface.unRegistLitener(listener);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                aidlInterface = null;
            }
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != aidlInterface && aidlInterface.asBinder().isBinderAlive()) {
            try {
                aidlInterface.unRegistLitener(listener);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
        unbindService(serviceConnection);
    }

    private void bind() {
        Intent intent = new Intent(action);
        intent.setPackage("cn.wyf.myaidlserver");
        bindService(intent, serviceConnection, BIND_AUTO_CREATE);
    }
}
